// ignore_for_file: file_names, slash_for_doc_comments, must_call_super, unnecessary_overrides, override_on_non_overriding_member, must_be_immutable, prefer_const_constructors, sized_box_for_whitespace, avoid_unnecessary_containers, avoid_print

import 'package:desktop_webview_window/desktop_webview_window.dart';
import 'package:flutter/material.dart';
import 'package:mybase/base/abstract/IFFR.dart';
import 'package:mybase/base/abstract/IFFRefresh.dart';
import 'package:mybase/base/abstract/IFFScaffold.dart';
import 'package:mybase/base/super/FFInk.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFTextButton.dart';
import 'package:mybase/base/widget/page/FFScaffold.dart';
import 'package:mybase/base/tool/extension/FFListExt.dart';
import 'package:webview_window_example/pages/androidUpdateXml/AndroidUpdateXmlEvent.dart';
import 'package:webview_window_example/utils/CommonUtils.dart';
import 'package:webview_window_example/utils/navi/AppNavi.dart';
import 'package:webview_window_example/entity/TranslateEntity.dart';
import 'package:webview_window_example/textField/FFMoreTextField.dart';

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';

class AndroidUpdateXmlPage extends FFScaffold {
  String? name;

  AndroidUpdateXmlPage({
    Key? key,
    this.name,
  }) : super(key: key);

  @override
  AndroidUpdateXmlPageState createState() => AndroidUpdateXmlPageState();
}

class AndroidUpdateXmlPageState extends FFScaffoldState
    with MFFScaffold
    implements IWebviewWindow {
  /**固定写法 */
  @override
  IFFScaffold get iffScaffold => this;

  @override
  AndroidUpdateXmlPage get widget => super.widget as AndroidUpdateXmlPage;

  @override
  IFFRefresh? get iffRefresh => refresh;
  late AndroidUpdateXmlPageRefresh refresh =
      AndroidUpdateXmlPageRefresh(state: this);

  /**固定写法end */

  final TextEditingController textController = TextEditingController();
  final TextEditingController resultController = TextEditingController();

  int selectIndex = 0;

  bool? webviewAvailable;
  late Webview webview;

  String? targetDir;

  @override
  void ffInitState() {
    super.ffInitState();
    WebviewWindow.isWebviewAvailable().then((value) {
      setState(() {
        webviewAvailable = value;
      });
    });
  }

  @override
  void ffDispose() {
    textController.dispose();
    super.ffDispose();
  }

  @override
  Widget? ffNavi(BuildContext context) {
    return AppNavi.createNavi(
      context,
      title: "android 更新xml",
    );
  }

  @override
  Widget? ffBody(BuildContext context) {
    List<Widget> slivers = [
      SliverToBoxAdapter(
        child: Container(
          child: FFLineContainer(
            children: [
              FFTextButton(
                text: "待翻译英文",
                fontSize: 16,
                margin: EdgeInsets.only(top: 10),
              ),
              Container(
                color: Colors.pink,
                padding: const EdgeInsets.all(16),
                child: FFMoreTextField(
                  controller: textController,
                  onValid: (bool valid, String value) {},
                  min: 0,
                  max: 999999,
                  height: 100,
                  placeStr: "1.value2\n2.value2",
                  placeTextStyle: const TextStyle(color: Colors.grey),
                ),
              ),
              FFTextButton(
                text: "结果",
                fontSize: 16,
                margin: const EdgeInsets.only(top: 16),
              ),
              Container(
                color: Colors.pink,
                padding: const EdgeInsets.all(16),
                child: FFMoreTextField(
                  controller: resultController,
                  onValid: (bool valid, String value) {},
                  min: 0,
                  max: 999999,
                  height: 100,
                  placeStr: "这儿是翻译结果",
                  placeTextStyle: const TextStyle(color: Colors.grey),
                ),
              ),
              Container(
                color: Colors.green.withAlpha(100),
                margin: EdgeInsets.only(top: 8, bottom: 16),
                child: Wrap(
                  spacing: 16,
                  runSpacing: 8,
                  children: () {
                    List<Widget> list = [];

                    for (int i = 0; i < entitys.length; i++) {
                      TranslateEntity entity = entitys[i];
                      list.add(FFInk(
                        child: FFTextButton(
                          unconstrainedBox: true,
                          decoration: BoxDecoration(
                            color: selectIndex == i
                                ? Colors.red
                                : Colors.red.withAlpha(50),
                          ),
                          text: entity.title,
                          fontSize: 16,
                          padding: const EdgeInsets.symmetric(
                              horizontal: 16, vertical: 8),
                          onClick: () {
                            selectIndex = i;
                            setState(() {});
                          },
                        ),
                      ));
                    }

                    return list;
                  }(),
                ),
              ),
              FFLineContainer(
                direction: Axis.horizontal,
                children: [
                  FFInk(
                    child: FFTextButton(
                      unconstrainedBox: true,
                      text: "下一个",
                      decoration: BoxDecoration(
                        color: Colors.green,
                      ),
                      onClick: onNext,
                      padding:
                          EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                      margin: EdgeInsets.only(top: 16),
                    ),
                  ),
                  SizedBox(
                    width: 8,
                  ),
                  FFInk(
                    child: FFTextButton(
                      unconstrainedBox: true,
                      text: "翻译",
                      decoration: BoxDecoration(
                        color: Colors.green,
                      ),
                      onClick: webviewAvailable != true ? null : onStart,
                      padding:
                          EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                      margin: EdgeInsets.only(top: 16),
                    ),
                  ),
                  SizedBox(
                    width: 8,
                  ),
                  FFInk(
                    child: FFTextButton(
                      unconstrainedBox: true,
                      text: "加入",
                      decoration: BoxDecoration(
                        color: Colors.green,
                      ),
                      onClick: onAdd,
                      padding:
                          EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                      margin: EdgeInsets.only(top: 16),
                    ),
                  ),
                ],
              ),
              FFInk(
                child: FFTextButton(
                  unconstrainedBox: true,
                  text: "选择保存目录",
                  decoration: BoxDecoration(
                    color: Colors.green,
                  ),
                  onClick: () async {
                    await onSelectTargetFile();
                  },
                  padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                  margin: EdgeInsets.only(top: 16),
                ),
              ),
              FFInk(
                child: FFTextButton(
                  text: "选择导出目录 ${targetDir ?? ""}",
                  maxLines: 10,
                  fontSize: 10,
                  margin: EdgeInsets.symmetric(horizontal: 16),
                  padding: EdgeInsets.symmetric(vertical: 8),
                  fontColor: Colors.white,
                  decoration: BoxDecoration(
                    color: Colors.green,
                  ),
                ),
              ),
              FFInk(
                child: FFTextButton(
                  unconstrainedBox: true,
                  text: "导出文件",
                  decoration: BoxDecoration(
                    color: Colors.green,
                  ),
                  onClick: onClickWrite,
                  padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                  margin: EdgeInsets.only(top: 16),
                ),
              ),
            ],
          ),
        ),
      ),
    ];
    return CustomScrollView(slivers: slivers);
  }

  ///通知页面加载完成，可以提取翻译了
  @override
  didFinished(int viewId) async {
    await Future.delayed(const Duration(milliseconds: 350));
    extractTranslate();
  }
}

class AndroidUpdateXmlEntity {}

///刷新组件
class AndroidUpdateXmlPageRefresh with MFFRefresh {
  AndroidUpdateXmlPageState state;

  AndroidUpdateXmlPageRefresh({required this.state});

  @override
  bool? ffCanRefreshHead() {
    return true;
  }

  @override
  ffRefresh(bool isHead) {
    ffRefreshResult(isHead, test, state);
  }

  //具体请求业务
  Future<IFFR> test(IRefreshParam? iRefreshParam) async {
    List? list;
    await Future.wait([
      //HomeApi.index_recentlyPlayed(),
    ]).then((value) {
      list = value;
    });

    IFFR? r = list?.safeValue(0);
    return r ?? R(rStatus: RStatus.result);
  }
}
